﻿namespace TestSapco
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using SAP.Middleware.Connector;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("[START]");

            // RfcDestination -> Repository
            //SAPB2E sap = new SAPB2E();
            //sap.AuthenticatePassword("S8433008Fasd", "DSADASD");
            //DataTable dt0 = sap.GetSapQualification();
            //string id = "KEPPELLANDzhixian.ong";
            //string newId;
            //if (id.IndexOf("\\") == -1)
            //{
            //    newId = id;
            //}
            //else
            //{
            //    newId = id.Remove(0, id.IndexOf("\\") + 1);
            //}

            test();

            Console.WriteLine("[END]");
            Console.ReadLine();
        }

        public static void test()
        {

            SAPB2E sap = new SAPB2E();
            DataTable dt0 = sap.GetDataTableFromFunction("Z_HR_QUALIFICATION_VALUES");
            DataTable dt1 = sap.GetDataTableFromFunction("Z_HR_BRANCH_OF_STUDY_VALUES");
            DataTable dt2 = sap.GetDataTableFromFunction("Z_HR_NS_STATUS_VALUES");
            DataTable dt4 = sap.GetDataTableFromFunction("Z_HR_NS_RANK_VALUES");
            DataTable dt5 = sap.GetDataTableFromFunction("Z_HR_ETHNIC_ORGIN_VALUES");
            DataTable dt6 = sap.GetDataTableFromFunction("Z_HR_DIALECT_VALUES");
            DataTable dt7 = sap.GetDataTableFromFunction("Z_HR_RELATIONSHIP_VALUES");
            DataTable dt8 = sap.GetDataTableFromFunction("Z_HR_HOBBY_CAT_VALUES");
            DataTable dt9 = sap.GetDataTableFromFunction("Z_HR_HOBBY_DESC_VALUES");
            DataTable dt10 = sap.GetDataTableFromFunction("Z_HR_SKILL_CAT_VALUES");
            DataTable dt11 = sap.GetDataTableFromFunction("Z_HR_SKILL_HEADER_VALUES");
            DataTable dt12 = sap.GetDataTableFromFunction("Z_HR_SKILL_DETAIL_VALUES");
            DataTable dt3 = sap.GetDataTableFromFunction("Z_HR_NS_VOCATION_VALUES");

            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SRM"].ConnectionString))
            {
                System.Data.DataSet ds = new DataSet();
                System.Data.SqlClient.SqlCommand cmd = conn.CreateCommand();
                System.Data.SqlClient.SqlDataAdapter da = new SqlDataAdapter(cmd);
                cmd.CommandText = "SELECT * FROM SAP_CodeType WHERE SapFunctionName IS NOT NULL";
                //cmd.CommandText = "SELECT * FROM SAP_CodeType WHERE Id = 'QUA' OR Id = 'DIS' OR Id = 'SKD'";
                da.Fill(ds);
                DataTable dtCodeTypes = ds.Tables[0];

                conn.Open();
                foreach (DataRow row in dtCodeTypes.Rows)
                {
                    DataTable sapDataTable = sap.GetDataTableFromFunction(
                        row.Field<string>("SapFunctionName"),
                        row.Field<string>("Id"),
                        row.Field<bool>("HasParentId1"),
                        row.Field<bool>("HasParentId2"));

                    //using (System.Data.SqlClient.SqlTransaction sqlTran = conn.BeginTransaction())
                    //{
                    //    cmd.CommandText = string.Format("DELETE FROM SAP_Code WHERE CodeTypeId = '{0}'", row["Id"].ToString());
                    //    cmd.Transaction = sqlTran;
                    //    cmd.ExecuteNonQuery();

                    //    using (System.Data.SqlClient.SqlBulkCopy bcp = new System.Data.SqlClient.SqlBulkCopy(conn, System.Data.SqlClient.SqlBulkCopyOptions.Default, sqlTran))
                    //    {
                    //        bcp.DestinationTableName = "SAP_Code";
                    //        bcp.WriteToServer(sapDataTable);
                    //    }

                    //    sqlTran.Commit();
                    //} // using (System.Data.SqlClient.SqlTransaction sqlTran = conn.BeginTransaction())

                } // foreach (DataRow row in dtCodeTypes.Rows)
            } // using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SRM"].ConnectionString))
        }
    }
}
